<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>tbox中数据库的使用</title>
    <meta name="description" content="tbox目前支持sqlite3、mysql两种关系型数据库（需要链接对应的libsqlite3.a和libmysql.a），并对其接口进行了封装，使用更加的方便简洁并且只需要换个url，就可以随时切换成其他数据库引擎，而不需要修改接口。下面先看个简单的例子：    /* 初始化一个mysql数据库     *  ...">

    
    <meta name="keywords" content="tbox,数据库,sqlite3,mysql,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2016/09/18/database-usage/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>tbox中数据库的使用</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2016-09-18
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#tbox" title="Category: tbox" rel="category">tbox</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a-->
        <a href="/cn/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a>&nbsp;
    
        <!--a href="/tag/#%E6%95%B0%E6%8D%AE%E5%BA%93" title="Tag: 数据库" rel="tag">数据库</a-->
        <a href="/cn/tag/#数据库" title="Tag: 数据库" rel="tag">数据库</a>&nbsp;
    
        <!--a href="/tag/#sqlite3" title="Tag: sqlite3" rel="tag">sqlite3</a-->
        <a href="/cn/tag/#sqlite3" title="Tag: sqlite3" rel="tag">sqlite3</a>&nbsp;
    
        <!--a href="/tag/#mysql" title="Tag: mysql" rel="tag">mysql</a-->
        <a href="/cn/tag/#mysql" title="Tag: mysql" rel="tag">mysql</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p>tbox目前支持sqlite3、mysql两种关系型数据库（需要链接对应的libsqlite3.a和libmysql.a），并对其接口进行了封装，使用更加的方便简洁并且只需要换个url，就可以随时切换成其他数据库引擎，而不需要修改接口。</p>

<p>下面先看个简单的例子：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="cm">/* 初始化一个mysql数据库
     *
     * localhost:   主机名，也可以是ip地址
     * type:        数据库的类型，目前支持：mysql 和 sqlite3两种类型
     * username:    数据库用户名
     * password:    数据库用户密码
     * database:    要访问的数据库名称， 如果不设置则自动连接默认的数据库
     *
     * 若果你想指定个端口，可以显示传入：
     * "sql://localhost:3306/?type=mysql&amp;username=xxxx&amp;password=xxxx&amp;database=xxxx"
     *
     * sqlite3的数据库访问url为：
     * "sql:///home/file.sqlitedb?type=sqlite3"
     *
     * 或者直接传文件路径：
     * "/home/file.sqlite3"
     * "file:///home/file.sqlitedb"
     * "C://home/file.sqlite3"
     */</span>
    <span class="n">tb_database_sql_ref_t</span> <span class="n">database</span> <span class="o">=</span> <span class="n">tb_database_sql_init</span><span class="p">(</span><span class="s">"sql://localhost/?type=mysql&amp;username=xxxx&amp;password=xxxx&amp;database=test"</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">database</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 打开数据库
</span>        <span class="k">if</span> <span class="p">(</span><span class="n">tb_database_sql_open</span><span class="p">(</span><span class="n">database</span><span class="p">))</span>
        <span class="p">{</span>
            <span class="c1">// 执行sql语句，进行查询
</span>            <span class="k">if</span> <span class="p">(</span><span class="n">tb_database_sql_done</span><span class="p">(</span><span class="n">database</span><span class="p">,</span> <span class="s">"select * from test"</span><span class="p">))</span>
            <span class="p">{</span>
                <span class="cm">/* 加载结果集
                 *
                 * 如果是insert, update等非select语句执行，这个时候返回tb_null， 说明没有结果集
                 *
                 * 这个结果集完全采用迭代器模式，方便快速迭代访问。
                 *
                 * 后面的参数tb_true指示尽量一次性加载所有结果到内存，如果成功
                 * 就可以通过tb_iterator_size(result)获取实际的结果集行数。
                 *
                 * 如果一次性加载失败或者传入的是tb_false， 说明只能通过fetch，一行行的回滚数据行
                 * 这样占用内存的资源较少，但是没法提前获取到实际的行数，这个时候tb_iterator_size(result)
                 * 返回的是一个超大值
                 */</span>
                <span class="n">tb_iterator_ref_t</span> <span class="n">result</span> <span class="o">=</span> <span class="n">tb_database_sql_result_load</span><span class="p">(</span><span class="n">database</span><span class="p">,</span> <span class="n">tb_true</span><span class="p">);</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">result</span><span class="p">)</span>
                <span class="p">{</span>
                    <span class="c1">// 如果一次性加载成功，返回实际的结果行数
</span>                    <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"row: size: %lu"</span><span class="p">,</span> <span class="n">tb_iterator_size</span><span class="p">(</span><span class="n">result</span><span class="p">));</span>
                    
                    <span class="c1">// 遍历所有行
</span>                    <span class="n">tb_for_all_if</span> <span class="p">(</span><span class="n">tb_iterator_ref_t</span><span class="p">,</span> <span class="n">row</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">row</span><span class="p">)</span>
                    <span class="p">{</span>
                        <span class="c1">// 显示行的列数
</span>                        <span class="n">tb_trace_i</span><span class="p">(</span><span class="s">"[row: %lu, col: size: %lu]: "</span><span class="p">,</span> <span class="n">row_itor</span><span class="p">,</span> <span class="n">tb_iterator_size</span><span class="p">(</span><span class="n">row</span><span class="p">));</span>
                        
                        <span class="c1">// 遍历这一行中的所有值
</span>                        <span class="n">tb_for_all_if</span> <span class="p">(</span><span class="n">tb_database_sql_value_t</span><span class="o">*</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">row</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
                        <span class="p">{</span>
                            <span class="cm">/* tb_database_sql_value_name(value): 获取值所对应列名
                             * tb_database_sql_value_text(value)：获取值的文本数据，如果是text类型的话
                             * tb_database_sql_value_type(value)：获取值的类型
                             *
                             * ...
                             */</span>
                            <span class="n">tb_tracet_i</span><span class="p">(</span><span class="s">"[%s:%s] "</span><span class="p">,</span> <span class="n">tb_database_sql_value_name</span><span class="p">(</span><span class="n">value</span><span class="p">),</span> <span class="n">tb_database_sql_value_text</span><span class="p">(</span><span class="n">value</span><span class="p">));</span>
                        <span class="p">}</span>
                    <span class="p">}</span>
                    
                    <span class="c1">// 释放结果集数据
</span>                    <span class="n">tb_database_sql_result_exit</span><span class="p">(</span><span class="n">database</span><span class="p">,</span> <span class="n">result</span><span class="p">);</span>
                <span class="p">}</span>
            <span class="p">}</span>
            <span class="k">else</span>
            <span class="p">{</span>
                <span class="c1">// 执行失败，显示失败状态和原因
</span>                <span class="n">tb_trace_e</span><span class="p">(</span><span class="s">"done %s failed, error: %s"</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">tb_state_cstr</span><span class="p">(</span><span class="n">tb_database_sql_state</span><span class="p">(</span><span class="n">database</span><span class="p">)));</span>
            <span class="p">}</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="c1">// 打开失败，显示失败状态和原因
</span>            <span class="n">tb_trace_e</span><span class="p">(</span><span class="s">"open failed: %s"</span><span class="p">,</span> <span class="n">tb_state_cstr</span><span class="p">(</span><span class="n">tb_database_sql_state</span><span class="p">(</span><span class="n">database</span><span class="p">)));</span>
        <span class="p">}</span>
        
        <span class="c1">// 退出数据库
</span>        <span class="n">tb_database_sql_exit</span><span class="p">(</span><span class="n">database</span><span class="p">);</span>
    <span class="p">}</span>
</code></pre>
</div>

<p>上面的例子没法处理二进制数据，如果要处理二进制数据，比如插入一个图像数据什么的可以使用statement模式，预编译一个sql语句，bind每个参数来实现，这种方式更加的灵活而且对于频繁处理某个sql语句的情况，效率更高，因为省去每次都要解析一遍sql语法的过程。</p>

<p>具体使用过程也很简单，下面先看个insert二进制数据的例子：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// done
</span>    <span class="n">tb_database_sql_statement_ref_t</span>     <span class="n">stmt</span> <span class="o">=</span> <span class="n">tb_null</span><span class="p">;</span>
    <span class="n">tb_stream_ref_t</span>                     <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_null</span><span class="p">;</span>
    <span class="k">do</span>
    <span class="p">{</span>
        <span class="c1">// 初始化一个插入语句，需要自定义传入的参数用?代替， 并进行预编译处理
</span>        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">stmt</span> <span class="o">=</span> <span class="n">tb_database_sql_statement_init</span><span class="p">(</span><span class="n">database</span><span class="p">,</span> <span class="s">"insert into table2 values(?, ?, ?, ?, ?, ?, ?)"</span><span class="p">)))</span>
        <span class="p">{</span>
            <span class="c1">// trace
</span>            <span class="n">tb_trace_e</span><span class="p">(</span><span class="s">"stmt: init %s failed, error: %s"</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">tb_state_cstr</span><span class="p">(</span><span class="n">tb_database_sql_state</span><span class="p">(</span><span class="n">database</span><span class="p">)));</span>
            <span class="k">break</span> <span class="p">;</span>
        <span class="p">}</span>
        
        <span class="c1">// 初始化一个超大图像数据流
</span>        <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_url</span><span class="p">(</span><span class="s">"/tmp/large_image.png"</span><span class="p">);</span>
        <span class="n">tb_assert_and_check_break</span><span class="p">(</span><span class="n">stream</span><span class="p">);</span>
        
        <span class="c1">// 打开流
</span>        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">tb_stream_open</span><span class="p">(</span><span class="n">stream</span><span class="p">))</span> <span class="k">break</span><span class="p">;</span>
        
        <span class="c1">// 参数列表
</span>        <span class="n">tb_database_sql_value_t</span> <span class="n">list</span><span class="p">[</span><span class="mi">7</span><span class="p">];</span>

        <span class="c1">// 绑定一个text类型的参数，sql中对应：text
</span>        <span class="n">tb_database_sql_value_set_text</span><span class="p">(</span><span class="o">&amp;</span><span class="n">list</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s">"hello"</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>

        <span class="c1">// 绑定一个uint16大小的blob数据参数，由于是小块数据，不用使用stream，sql中对应：blob
</span>        <span class="n">tb_database_sql_value_set_blob16</span><span class="p">(</span><span class="o">&amp;</span><span class="n">list</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">data</span><span class="p">,</span> <span class="n">size</span><span class="p">);</span>

        <span class="c1">// 绑定一个uint8大小的超小数据块，节省存储空间，sql中对应：tinyblob
</span>        <span class="n">tb_database_sql_value_set_blob8</span><span class="p">(</span><span class="o">&amp;</span><span class="n">list</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">data</span><span class="p">,</span> <span class="n">size</span><span class="p">);</span>

        <span class="c1">// 绑定一个uint32大小的超大数据块，sql中对应：longblob
</span>        <span class="n">tb_database_sql_value_set_blob32</span><span class="p">(</span><span class="o">&amp;</span><span class="n">list</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">data</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">tb_null</span><span class="p">);</span>

        <span class="c1">// 绑定一个uint32大小的超大数据流，这样比较节省内存，不会一次性加载数据到内存中来处理，sql中对应：longblob
</span>        <span class="n">tb_database_sql_value_set_blob32</span><span class="p">(</span><span class="o">&amp;</span><span class="n">list</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="n">tb_null</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">stream</span><span class="p">);</span>

        <span class="c1">// 绑定一个int32整数，sql中对应int
</span>        <span class="n">tb_database_sql_value_set_int32</span><span class="p">(</span><span class="o">&amp;</span><span class="n">list</span><span class="p">[</span><span class="mi">5</span><span class="p">],</span> <span class="n">number</span><span class="p">);</span>

        <span class="c1">// 绑定一个int16整数，sql中对应smallint
</span>        <span class="n">tb_database_sql_value_set_int16</span><span class="p">(</span><span class="o">&amp;</span><span class="n">list</span><span class="p">[</span><span class="mi">6</span><span class="p">],</span> <span class="n">snumber</span><span class="p">);</span>
        
        <span class="c1">// 绑定参数列表
</span>        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">tb_database_sql_statement_bind</span><span class="p">(</span><span class="n">database</span><span class="p">,</span> <span class="n">stmt</span><span class="p">,</span> <span class="n">list</span><span class="p">,</span> <span class="n">tb_arrayn</span><span class="p">(</span><span class="n">list</span><span class="p">)))</span>
        <span class="p">{</span>
            <span class="c1">// trace
</span>            <span class="n">tb_trace_e</span><span class="p">(</span><span class="s">"stmt: bind %s failed, error: %s"</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">tb_state_cstr</span><span class="p">(</span><span class="n">tb_database_sql_state</span><span class="p">(</span><span class="n">database</span><span class="p">)));</span>
            <span class="k">break</span> <span class="p">;</span>
        <span class="p">}</span>
        
        <span class="c1">// 执行预编译的sql语句
</span>        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">tb_database_sql_statement_done</span><span class="p">(</span><span class="n">database</span><span class="p">,</span> <span class="n">stmt</span><span class="p">))</span>
        <span class="p">{</span>
            <span class="c1">// trace
</span>            <span class="n">tb_trace_e</span><span class="p">(</span><span class="s">"stmt: done %s failed, error: %s"</span><span class="p">,</span> <span class="n">sql</span><span class="p">,</span> <span class="n">tb_state_cstr</span><span class="p">(</span><span class="n">tb_database_sql_state</span><span class="p">(</span><span class="n">database</span><span class="p">)));</span>
            <span class="k">break</span> <span class="p">;</span>
        <span class="p">}</span>
        
    <span class="p">}</span> <span class="k">while</span> <span class="p">(</span><span class="mi">0</span><span class="p">);</span>

    <span class="c1">// 删除sql语句对象
</span>    <span class="k">if</span> <span class="p">(</span><span class="n">stmt</span><span class="p">)</span> <span class="n">tb_database_sql_statement_exit</span><span class="p">(</span><span class="n">database</span><span class="p">,</span> <span class="n">stmt</span><span class="p">);</span>

    <span class="c1">// 退出数据流
</span>    <span class="k">if</span> <span class="p">(</span><span class="n">stream</span><span class="p">)</span> <span class="n">tb_stream_exit</span><span class="p">(</span><span class="n">stream</span><span class="p">);</span>
</code></pre>
</div>

<p>对于获取对应结果集数据，使用的接口和第一个例子一样，可以直接进行迭代
也可以通过索引直接访问其中某列的数据：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 按回滚方式加载结果集
</span>    <span class="n">tb_iterator_ref_t</span> <span class="n">result</span> <span class="o">=</span> <span class="n">tb_database_sql_result_load</span><span class="p">(</span><span class="n">database</span><span class="p">,</span> <span class="n">tb_false</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">result</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 遍历行
</span>        <span class="n">tb_for_all_if</span> <span class="p">(</span><span class="n">tb_iterator_ref_t</span><span class="p">,</span> <span class="n">row</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">row</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="c1">// 获取text数据
</span>            <span class="n">tb_database_sql_value_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">name</span> <span class="o">=</span> <span class="n">tb_iterator_item</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
            <span class="n">tb_assert_and_check_break</span><span class="p">(</span><span class="n">name</span><span class="p">);</span>
            <span class="n">tb_tracet_i</span><span class="p">(</span><span class="s">"[%s:%s] "</span><span class="p">,</span> <span class="n">tb_database_sql_value_name</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">tb_database_sql_value_text</span><span class="p">(</span><span class="n">name</span><span class="p">));</span>
            
            <span class="c1">// 获取blob16数据
</span>            <span class="n">tb_database_sql_value_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">data</span> <span class="o">=</span> <span class="n">tb_iterator_item</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
            <span class="n">tb_assert_and_check_break</span><span class="p">(</span><span class="n">data</span><span class="p">);</span>
            <span class="n">tb_tracet_i</span><span class="p">(</span><span class="s">"[%s:%s] "</span><span class="p">,</span> <span class="n">tb_database_sql_value_name</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">tb_database_sql_value_blob</span><span class="p">(</span><span class="n">data</span><span class="p">));</span>
            
            <span class="c1">// 获取blob32数据
</span>            <span class="n">tb_database_sql_value_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">ldata</span> <span class="o">=</span> <span class="n">tb_iterator_item</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="mi">4</span><span class="p">);</span>
            <span class="n">tb_assert_and_check_break</span><span class="p">(</span><span class="n">ldata</span><span class="p">);</span>
            <span class="p">{</span>
                <span class="cm">/* 优先尝试获取数据buffer，如果存在的话
                 *
                 * 针对小块数据进行了优化，就算是blob32的数据，如果数据大小
                 * 本身不大，会直接存到内存buffer中去，来快速读取
                 */</span>
                <span class="n">tb_stream_ref_t</span>     <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_null</span><span class="p">;</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">tb_database_sql_value_blob</span><span class="p">(</span><span class="n">ldata</span><span class="p">))</span>
                <span class="p">{</span>
                    <span class="c1">// 获取数据和大小
</span>                    <span class="n">tb_byte_t</span> <span class="k">const</span><span class="o">*</span>    <span class="n">data</span> <span class="o">=</span> <span class="n">tb_database_sql_value_blob</span><span class="p">(</span><span class="n">ldata</span><span class="p">);</span>
                    <span class="n">tb_size</span>             <span class="n">size</span> <span class="o">=</span> <span class="n">tb_database_sql_value_size</span><span class="p">(</span><span class="n">ldata</span><span class="p">);</span>
                    <span class="c1">// ...
</span>
                <span class="p">}</span>
                <span class="c1">// 如果获取不到数据buffer，说明数据量很大，尝试获取数据流来处理
</span>                <span class="k">else</span> <span class="k">if</span> <span class="p">((</span><span class="n">stream</span> <span class="o">=</span> <span class="n">tb_database_sql_value_blob_stream</span><span class="p">(</span><span class="n">ldata</span><span class="p">)))</span>
                <span class="p">{</span>
                    <span class="c1">// 进行stream的数据读取，注：这个流不支持写操作，只能用来读
</span>                    <span class="c1">// ...
</span>
                <span class="p">}</span>
            <span class="p">}</span>
            
            <span class="c1">// 获取int32数据，如果是int8, int16 或者float类型的数据，会自动进行强转
</span>            <span class="n">tb_database_sql_value_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">number</span> <span class="o">=</span> <span class="n">tb_iterator_item</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="mi">5</span><span class="p">);</span>
            <span class="n">tb_assert_and_check_break</span><span class="p">(</span><span class="n">number</span><span class="p">);</span>
            <span class="n">tb_tracet_i</span><span class="p">(</span><span class="s">"[%s:%d] "</span><span class="p">,</span> <span class="n">tb_database_sql_value_name</span><span class="p">(</span><span class="n">number</span><span class="p">),</span> <span class="n">tb_database_sql_value_int32</span><span class="p">(</span><span class="n">number</span><span class="p">));</span>
            
        <span class="p">}</span>
        
        <span class="c1">// 退出结果集
</span>        <span class="n">tb_database_sql_result_exit</span><span class="p">(</span><span class="n">database</span><span class="p">,</span> <span class="n">result</span><span class="p">);</span>
    <span class="p">}</span>
</code></pre>
</div>

        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
                    
                    <h2 id="english">English</h2>
                    <ul>
                    
                    <li class="relatedPost">
                        <a href="/2016/09/18/database-usage/">Access sql database using tbox
                        
                        </a>
                    </li>
                    
                    
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        
            </ul>
        

        
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
                    
                
            
            
        
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2016/09/18/custom-printf/">使用printf定制化打印对象</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2016/09/28/thread-local/">线程局部存储tls的使用</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
